// component/database/weaponList/weaponList.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    myProperty: { // 属性名
      type: String,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    weaponList: [],
    activeNames: -1,
    current: 1,
    pageState: true

  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      this.getWeaponList(this.data.current)
      console.log(this.properties.myProperty)
    },
    moved: function () { },
    detached: function () { },
    ready: function () {
    },

  },

  /**
   * 组件的方法列表
   */
  methods: {
    getWeaponList(page) {
      var that = this
      wx.request({
        url: 'http://daxian.viphk.ngrok.org/api/getArmor', //仅为示例，并非真实的接口地址
        data: {
          page: page
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success(res) {
          // console.log(res.data)
          that.setData({
            weaponList: res.data,
            pageState: false
          })
        }
      })
    },
    selectCollapse(event) {
      var i = this.data.activeNames
      var id = event.currentTarget.dataset.id
      if (i === id) {
        this.setData({
          activeNames: -1
        })
      } else {
        this.setData({
          activeNames: id
        })
      }
    },
    handleChange({ detail }) {
      const type = detail.type;
      if (type === 'next') {
        this.setData({
          current: this.data.current + 1,
          weaponList: [],
          pageState: true,
        });
        this.getWeaponList(this.data.current);
      } else if (type === 'prev') {
        this.setData({
          current: this.data.current - 1,
          weaponList: [],
          pageState: true
        });
        this.getWeaponList(this.data.current);

      }
    },
    goToArmorInfo(e) {
      // console.log(e.currentTarget.dataset.id);
      wx.navigateTo({
        url: '/pages/armorInfo/armorInfo?id=' + encodeURIComponent(e.currentTarget.dataset.id),
      })
    }
  }
})
